XtGem Forum catalog

public class Primes {


public class primes
{
public static void main(String[]args)
{
getPro();
}
private static void getPro()
{
String primeNumbers="";
int x,y;
for(x=1;x<=100;x++)
{
int ctr=0;
for(y=x;y>=1;y--)
{
if(x%y==0)
{
ctr+=1;
}
}
if(ctr==2)
{

primeNumbers+=x+" ";
}
}

System.out.print("The Prime Numbers from 1-100 are : \n"+primeNumbers);
}
}

public static void main(String[] args) {
for(int i = 2; i < 100; i++) {
if(isPrime(i)) {
System.out.println(i);
}
}
}

public static boolean isPrime(int n) {
if(n == 2 || n == 3) return true;
else if(n % 6 != 1 && n % 6 != 5) return false;
else {
int s = (int) Math.sqrt(n) + 1;
for(int i = 7; i < s; i += 6) {
if(n % i == 0) return false;
if(n % (i + 2) == 0) return false;
}
return true;
}
}

}

%
% Using a for loop to create and store all the odd numbers
% between 1 and 1000
%
% Variables:
% i : the loop index.
% array : a list of integer numbers
%

for i = 1:2:1000 % here we change the default to count by 2
% that is what the :2: means (start:by:end)

array( ceil(i/2) ) = i;

end % for

% alternatively

for i = 1:500 % here we change count by 1
% and compute the appropriate odd value

array( i ) = i * 2 - 1;

end % for